package com.campus.secondhand.controller;

import java.security.Principal;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.campus.secondhand.common.Result;
import com.campus.secondhand.entity.User;
import com.campus.secondhand.model.dto.ShoppingCartDTO;
import com.campus.secondhand.model.vo.PageVO;
import com.campus.secondhand.model.vo.ShoppingCartVO;
import com.campus.secondhand.service.ShoppingCartService;
import com.campus.secondhand.service.UserService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 购物车控制器
 */
@RestController
@RequestMapping("/cart")
@RequiredArgsConstructor
@Slf4j
public class ShoppingCartController {

    private final ShoppingCartService shoppingCartService;
    private final UserService userService;

    /**
     * 获取购物车列表
     *
     * @param principal 当前登录用户
     * @param pageNum   页码
     * @param pageSize  每页数量
     * @return 购物车列表
     */
    @GetMapping("/list")
    @PreAuthorize("isAuthenticated()")
    public Result<PageVO<ShoppingCartVO>> getCartList(
            Principal principal,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        // 获取当前用户ID
        Result<User> userResult = userService.getByUsername(principal.getName());
        if (userResult.getCode() != 200) {
            return Result.error(userResult.getCode(), userResult.getMessage());
        }

        Long userId = userResult.getData().getId();
        return shoppingCartService.getCartList(userId, pageNum, pageSize);
    }

    /**
     * 添加商品到购物车
     *
     * @param principal 当前登录用户
     * @param dto       购物车数据
     * @return 添加结果
     */
    @PostMapping("/add")
    @PreAuthorize("isAuthenticated()")
    public Result<ShoppingCartVO> addToCart(
            Principal principal,
            @Validated @RequestBody ShoppingCartDTO dto) {

        // 获取当前用户ID
        Result<User> userResult = userService.getByUsername(principal.getName());
        if (userResult.getCode() != 200) {
            return Result.error(userResult.getCode(), userResult.getMessage());
        }

        // 设置用户ID
        dto.setUserId(userResult.getData().getId());
        return shoppingCartService.addToCart(dto);
    }

    /**
     * 从购物车移除商品
     *
     * @param principal 当前登录用户
     * @param id        购物车项ID
     * @return 移除结果
     */
    @PostMapping("/remove")
    @PreAuthorize("isAuthenticated()")
    public Result<Void> removeFromCart(
            Principal principal,
            @RequestParam Long id) {

        // 获取当前用户ID
        Result<User> userResult = userService.getByUsername(principal.getName());
        if (userResult.getCode() != 200) {
            return Result.error(userResult.getCode(), userResult.getMessage());
        }

        return shoppingCartService.removeFromCart(userResult.getData().getId(), id);
    }

    /**
     * 清空购物车
     *
     * @param principal 当前登录用户
     * @return 清空结果
     */
    @PostMapping("/clear")
    @PreAuthorize("isAuthenticated()")
    public Result<Void> clearCart(Principal principal) {
        // 获取当前用户ID
        Result<User> userResult = userService.getByUsername(principal.getName());
        if (userResult.getCode() != 200) {
            return Result.error(userResult.getCode(), userResult.getMessage());
        }

        return shoppingCartService.clearCart(userResult.getData().getId());
    }
}